package com.vm.activity;

import org.json.JSONObject;

import com.vm.activity.GetShareToQQParamsDialog;
import com.wwt.app.R;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月23日 下午2:53:07]
 * @Version: [v1.0]
 * 
 */
public class ShareActivity extends Activity {

	private Tencent mTencent;
	private static final String APP_ID = "222222";
	ImageView imgbtn_search_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.invite_install_layout);
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
		final Context ctxContext = this.getApplicationContext();
		mTencent = Tencent.createInstance(APP_ID, ctxContext);
		mHandler = new Handler();
	}

	@Override
	protected void onResume() {
		super.onResume();
		TextView tv_qq = (TextView) findViewById(R.id.tv_qq);
		tv_qq.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				onClickShareToQQ();
			}
		});

	}

//	private void onClickShareToQQ2() {
//		final Activity context = ShareActivity.this;
//		new GetShareToQQParamsDialog(context,
//				new GetShareToQQParamsDialog.ShareToQQParamsListener() {
//
//					@Override
//					public void onComplete(Bundle params) {
//
//						// 两种方式使用分享:1主线程，2子线程
//						/*
//						 * 主线程 doShareToQQ(params);
//						 */
//						/* 子线程 */
//						shareParams = params;
//						Thread thread = new Thread(shareThread);
//						thread.start();
//
//					}
//				}).show();
//
//	}

	private void onClickShareToQQ() {
		Bundle b = getShareBundle();
		if (b != null) {
			shareParams = b;
			Thread thread = new Thread(shareThread);
			thread.start();
		}
	}

	private Bundle getShareBundle() {
		Bundle bundle = new Bundle();
		bundle.putString("title", "万维通云应用手机App");
		bundle.putString("imageUrl",
				"http://www.91pcapp.com/site/qrcode/20153/19/com.example.vmclient.png");
		bundle.putString(
				"targetUrl",
				"http://www.91pcapp.com/downloadApk?softId=96f480a8-6911-46f0-a92f-11ea4a42304e");
		bundle.putString("summary", "万维通云应用手机App,一个专注于App推广的平台");
		bundle.putString("site", "2222");
		bundle.putString("appName", "万维通云应用手机App");
		return bundle;
	}

	Bundle shareParams = null;

	Handler shareHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
		}
	};

	// 线程类，该类使用匿名内部类的方式进行声明
	Runnable shareThread = new Runnable() {

		public void run() {
			doShareToQQ(shareParams);
			Message msg = shareHandler.obtainMessage();

			// 将Message对象加入到消息队列当中
			shareHandler.sendMessage(msg);

		}
	};

	private void doShareToQQ(Bundle params) {
		mTencent.shareToQQ(ShareActivity.this, params, new BaseUiListener() {
			protected void doComplete(JSONObject values) {
				showResult("shareToQQ:", "onComplete");
			}

			@Override
			public void onError(UiError e) {
				showResult("shareToQQ:", "onError code:" + e.errorCode
						+ ", msg:" + e.errorMessage + ", detail:"
						+ e.errorDetail);
			}

			@Override
			public void onCancel() {
				showResult("shareToQQ", "onCancel");
			}
		});
	}

	private class BaseUiListener implements IUiListener {

		@Override
		public void onComplete(JSONObject response) {
			// mBaseMessageText.setText("onComplete:");
			// mMessageText.setText(response.toString());
			doComplete(response);
		}

		protected void doComplete(JSONObject values) {

		}

		@Override
		public void onError(UiError e) {
			showResult("onError:", "code:" + e.errorCode + ", msg:"
					+ e.errorMessage + ", detail:" + e.errorDetail);
		}

		@Override
		public void onCancel() {
			showResult("onCancel", "");
		}
	}

	private Handler mHandler;

	private void showResult(final String base, final String msg) {
		mHandler.post(new Runnable() {

			@Override
			public void run() {
			}
		});
	}

	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();

	}

}
